//
//  UIWindow+Extension.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import UIKit

extension UIWindow {
    
   public func switchRootViewController(_ viewController: UIViewController,
                                        animated: Bool = true,
                                        duration: TimeInterval = 0.3,
                                        options: UIView.AnimationOptions = .transitionCrossDissolve,
                                        completion: ((Bool) -> Void)? = nil) {
        
        guard animated else {
            rootViewController = viewController
            completion?(true)
            return
        }
        
        UIView.transition(with: self, duration: duration, options: options, animations: { [unowned self] in
            let oldState = UIView.areAnimationsEnabled
            UIView.setAnimationsEnabled(false)
            self.rootViewController = viewController
            UIView.setAnimationsEnabled(oldState)
        }) { success in
            completion?(success)
        }
    }
}
